package com.xca.is.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.xca.is.R;
import com.xca.is.databinding.ActivitySoftwareDetalisBinding;
import com.xca.is.entity.SoftwareEntity;
import com.xca.is.utils.WebViewScriptUtils;
import com.xca.is.views.PrivacyPolicyActivity;

public class SoftwareDetailsActivity extends AppCompatActivity {
    private ActivitySoftwareDetalisBinding binding ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySoftwareDetalisBinding.inflate(getLayoutInflater());
        EdgeToEdge.enable(this);
        setContentView(binding.getRoot());

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        Intent intent = getIntent();
        String soft = intent.getStringExtra("software");
        Gson gson = new Gson() ;
        SoftwareEntity.DataDTO software = gson.fromJson(soft, SoftwareEntity.DataDTO.class) ;
        if (software != null){
            showData(software);
            onClickListener(software);
        }
    }
    private void showData(SoftwareEntity.DataDTO software){
        Glide.with(this)
                .load(software.getPic())
                .fitCenter()
                .error(R.drawable.image_error)
                .into(binding.softDetailIcon);
        binding.softDetailName.setText(software.getName());
        binding.softDetailAuthor.setText(software.getUsername());
        binding.softDetailVersion.setText(software.getVersion());
        binding.softDetailSize.setText(software.getSize());
        binding.softDetailType.setText(software.getCategoryname());
        binding.softDetailPrice.setText(software.getAppauthorization());
        WebViewScriptUtils.setWebView(binding.softDetailWeb,software.getDesc());
    }

    private void onClickListener(SoftwareEntity.DataDTO software){
        binding.softDetailPolicy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SoftwareDetailsActivity.this, PrivacyPolicyActivity.class);
                startActivity(intent);
            }
        });
        binding.softDetailCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clip = ClipData.newPlainText("soft", software.getDownload());
                clipboard.setPrimaryClip(clip);
                Toast.makeText(SoftwareDetailsActivity.this, "文本已复制", Toast.LENGTH_SHORT).show();
            }
        });
    }
}